package com.assessment.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.assessment.dao.AssessmentTemplateRepository;
import com.assessment.dto.AssessmentTemplateDTO;
import com.assessment.entity.AssessmentTemplate;

@Service
@Transactional
public class AssessmentTemplateService implements IAssessmentTemplateService{
	
	@Autowired
	private AssessmentTemplateRepository assessmentTemplateRepository;
	
	public void saveOrUpdate(AssessmentTemplate assessmentTemplate) {
		// TODO Auto-generated method stub
		System.out.println(assessmentTemplateRepository);
		assessmentTemplateRepository.save(assessmentTemplate);
	}

	public void delete(Long id) {
		// TODO Auto-generated method stub
		AssessmentTemplate assessmentTemplate=assessmentTemplateRepository.findOne(id);
		if(assessmentTemplate!=null) {			
			assessmentTemplateRepository.delete(assessmentTemplate);
		}
		
	}

	public void delete(List<Long> ids) {
		// TODO Auto-generated method stub
		List<AssessmentTemplate> assessmentTemplateList=(List<AssessmentTemplate>) assessmentTemplateRepository.findAll(ids);
		if(assessmentTemplateList!=null) {
			assessmentTemplateRepository.delete(assessmentTemplateList);
		}
		
	}
	
	@Transactional(readOnly=true)
	public AssessmentTemplate findOne(Long id) {
		// TODO Auto-generated method stub		
		return assessmentTemplateRepository.findOne(id);
		
	}
	@Transactional(readOnly=true)
	public List<AssessmentTemplate> findAll() {
		// TODO Auto-generated method stub
		return (List<AssessmentTemplate>) assessmentTemplateRepository.findAll();
	}
	
	@Transactional(readOnly=true)
	public Page<AssessmentTemplateDTO> findAll(Specification<AssessmentTemplate> spec, Pageable pageable) {
		// TODO Auto-generated method stub
		List<AssessmentTemplateDTO> results = new ArrayList<AssessmentTemplateDTO>();
		Page<AssessmentTemplate> page=assessmentTemplateRepository.findAll(spec, pageable);
		for(AssessmentTemplate assessmentTemplate:page.getContent()) {
			AssessmentTemplateDTO assessmentTemplateDTO = new AssessmentTemplateDTO();
        	BeanUtils.copyProperties(assessmentTemplate, assessmentTemplateDTO);
        	results.add(assessmentTemplateDTO);
		}
		return new PageImpl<AssessmentTemplateDTO>(results, pageable, page.getTotalElements());
	}

}
